using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UI_Message : MonoBehaviour
{
    public Image background;
    public Text message;

    Vector2 origin_size;
    Vector2 size;
    float weight = 0f;
    string state = "wait";

    string info = "";
    // Start is called before the first frame update
    void Start()
    {
        origin_size.x = 800f;
        origin_size.y = 140f;
    }

    int n = 0;
    void FixedUpdate()
    {
        switch (state)
        {
            case "show":
                {
                    message.text = info;
                    if (origin_size.x + 10 - weight > 0.5f)
                    {
                        weight += (origin_size.x + 10 - weight) * 0.1f;
                        size.x = weight;
                        background.GetComponent<RectTransform>().sizeDelta = size;
                    }
                    else
                    {
                        weight = origin_size.x + 10;
                        size.x = weight;
                        background.GetComponent<RectTransform>().sizeDelta = size;
                        state = "stay";
                    }
                    break;
                }
            case "close":
                {
                    if (weight - 0 > 1f)
                    {
                        weight -= (weight - 0) * 0.1f;
                        size.x = weight;
                        background.GetComponent<RectTransform>().sizeDelta = size;
                    }
                    else
                    {
                        weight = 0;
                        size.x = weight;
                        background.GetComponent<RectTransform>().sizeDelta = size;
                        gameObject.SetActive(false);
                        state = "wait";
                    }
                    break;
                }
            case "stay":
                {
                    if (n < 100)
                    {
                        n++;
                    }
                    else
                    {
                        n = 0;
                        state = "close";
                        message.text = "";
                    }
                    break;
                }
        }
    }

    public void Show(string message)
    {
        gameObject.SetActive(true);
        size = origin_size;
        size.x = weight;
        size.y = 140f;
        background.GetComponent<RectTransform>().sizeDelta = size;
        info = message;
        state = "show";
    }
}
